package Proxy;

import cong.cheng.thread.MyCallable;
import cong.cheng.thread.MyRunnable;
import cong.cheng.thread.MyThread;
import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Mytest02 {
    public static void main(String args[]) {
        // 创建线程对象
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();
        // 启动线程
        t1.start();
        t2.start();
    }

    @Test
    public void test01(){
        MyRunnable myRunnable = new MyRunnable();
        Thread t1 = new Thread(myRunnable);
        Thread t2 = new Thread(myRunnable);
        t1.start();
        t2.start();
    }

    @Test
    public void test03(){
        // 创建一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(1);

        // 创建MyCallable的实例
        MyCallable myCallable = new MyCallable();

        // 提交Callable任务并获取Future对象
        Future<Integer> future = executorService.submit(myCallable);

        try {
            // 从Future对象中获取异步计算的结果
            Integer result = future.get();
            System.out.println("Result: " + result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } finally {
            // 关闭线程池
            executorService.shutdown();
        }
    }
}
